﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DDOS.Game_State;
using DDOS.Camera;

namespace DDOS.Objects
{
    class Boss1 : Enemy
    {
        private Vector2 patrol_point;
        private Vector2 origin;
        Rectangle spriteRectangle, sourceRectangle0,sourceRectangle1;
        private const Decimal SPEED = 1m;
        int frame;
        int time;
        bool alarmed;
        Random ran;
        bool on_patrol;
        Player plyr;

        private BoundingCircle bounding_circle;
        public BoundingCircle boundingCircle
        {
            get { return bounding_circle; }
            set { bounding_circle = value; bounding_circle.parent = this; }
        }


        public void initCamera(Camera3D cam)
        {
            this.camera = cam;
        }

        public override void InitializeComponents()
        {
           
            alarmed = true;
            this.maxHealth = 5;
            this.health = 25;
            this.power = 5;
            this.speed = 1;
            this.sight = 1;
            on_patrol = true;
            isActive = true;
            this.origin = new Vector2(this.coordinates.X, this.coordinates.Y);
            this.patrol_point = new Vector2(ran.Next(100, 1000), ran.Next(50, 500));
            this.texture = ImageManager2D.Instance.Boss;
            boundingObject = new BoundingRectangle((int)coordinates.X, (int)coordinates.Y, (int)coordinates.X + 100, (int)coordinates.Y + 100);
            boundingCircle = new BoundingCircle(coordinates.X, coordinates.Y, (float)this.sight);
            spriteRectangle = new Rectangle((int)coordinates.X, (int)coordinates.Y, 100, 100);
            sourceRectangle0 = new Rectangle(0, 0, 200, 200);
            sourceRectangle1 = new Rectangle(200, 0, 400, 200);
            this.velocity = new Vector2(0, 0);
            blocked = false;
        }
 

        public void init_target(Player p)
        {
            this.plyr = p;
        }
        public void init_random(Random r)
        {
            ran = r;
        }

        private void patrol()
        {
            if ((this.coordinates - plyr.Coordinates).Length() >= (float)/*SPEED*/speed)
            {
                Vector2 v = (plyr.Coordinates - this.coordinates);
                v.Normalize();
                velocity = v * (float)speed;
            }
            else
            {
                this.patrol_point = new Vector2(ran.Next(100, 1000), ran.Next(50, 500));
            }
        }

        private void act()
        {

        }

        public override void Update(GameTime game_Time)
        {
            if (isActive)
            {
                velocity = Vector2.Zero;
                if (on_patrol)
                {
                    patrol();
                }
                act();
                coordinates += velocity;
                this.boundingObject.Move(this.velocity.X, this.velocity.Y);
                this.boundingCircle.Move(this.velocity.X, this.velocity.Y);
                this.spriteRectangle.X = (int)this.coordinates.X;
                this.spriteRectangle.Y = (int)this.coordinates.Y;
                if (alarmed)
                {
                    time += 1;
                }
                if (health <= 0)
                {
                    plyr.add_to_score(500);
                    SoundManager.Instance.Stop();
                    isActive = false;
                    TerminalLayer layer = new TerminalLayer(plyr);
                    TerminalScreen screen = new TerminalScreen();
                    screen.PushLayer(layer);
                    ScreenManager.Instance.LoadScreen(screen);
                }
            }
        }

        public override void Draw()
        {
            if (isActive)
            {
                if (alarmed)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle0, Color.White);

                }
                else
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle1, Color.White);
                }
            }
        }

    }
}
